package com.soundShape.mediators.login
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.views.login.Login;
	
	import flash.events.MouseEvent;
	
	import spark.components.TextInput;
	import spark.events.TextOperationEvent;
	
	
	
	
	public class LoginMediator extends AbstractViewMediator
	{
		
				private var lenTxtInAlias:int;
				private var lenTxtInPassword:int;
				
				public function LoginMediator()
				{
					super(Login);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Login
				{
					return this._view as Login;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					
					this.view.btnSubmit.addEventListener(MouseEvent.CLICK, handleBtnSubmit);
					
					this.view.btnDevLogin.addEventListener(MouseEvent.CLICK, handleBtnDevLogin);
					
					this.view.txtInAlias.addEventListener(TextOperationEvent.CHANGE, handleTextOperation);
					
					this.view.txtInPassword.addEventListener(TextOperationEvent.CHANGE, handleTextOperation);
					
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnNewEmployee(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_NEW_EMPLOYEE_VIEW));
				}
				
				private function handleBtnSubmit(mouseEvent:MouseEvent):void
				{
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.LOGIN);
					soundShapeEvent.data = {alias:this.view.txtInAlias.text, password:this.view.txtInPassword.text};
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				private function handleBtnDevLogin(mouseEvent:MouseEvent):void
				{
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.LOGIN);
					soundShapeEvent.data = {alias:"admin", password:"admin"};
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				
				
				private function handleTextOperation(textOperationEvent:TextOperationEvent):void
				{
					var textInput:TextInput = TextInput(textOperationEvent.currentTarget);
					(textInput.id == "txtInAlias")?lenTxtInAlias = textInput.text.length:lenTxtInPassword = textInput.text.length;
					(lenTxtInAlias > 3 && lenTxtInPassword > 3)?this.view.btnSubmit.enabled = true:this.view.btnSubmit.enabled = false;
				}
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				[EventHandler(event="SoundShapeEvent.LOGIN_RESULT")]
				public function doLoginResult(soundShapeEvent:SoundShapeEvent):void
				{
					
					if(soundShapeEvent.data.isValidLogin)
					{
						this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
						this.view.lblLoginInfo.text = "";
					}
					else
					{
						this.view.lblLoginInfo.text = "Incorrect Login";
						this.view.lblLoginInfo.setStyle("color",0xff0000);
					}
					
					this.view.txtInAlias.text = "";
					this.view.txtInPassword.text = "";
					this.view.btnSubmit.enabled = false;
					lenTxtInAlias = 0;
					lenTxtInPassword = 0;
				}
				
				
				
				
			
		}
		
	}
